package com.ruoyi.system.controller;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.util.ZipUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.zip.ZipOutputStream;

public class DownloadController {

    /**
     * 图片打包下载
     * @author: wangzhouchao
     */
    @RequestMapping(value = "/downloadPictureList", method = RequestMethod.GET)
    public void downloadPictureList(HttpServletResponse response) {

        long readyStart = System.currentTimeMillis();

        // *************  1. 获取到存在数据库中的图片的url  *************
        // 获取当前类的所在项目路径
        File file = null;
        try {
            file = new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (FileNotFoundException e) {
            throw new RuntimeException("获取根目录失败，无法获取文件目录！");
        }
        if(!file.exists()) {
            file = new File("");
        }
        String absolutePath = file.getAbsolutePath();

        // 要打包的文件夹列表
        String dirName = "";

        // *************  2. 创建要压缩的文件夹  *************
        // 根据订单号+国家名称+签证类型创建文件夹
        File dirOfOrder = new File(absolutePath, dirName);
        if(!dirOfOrder.exists()) {
            dirOfOrder.mkdirs();
        }

        ZipOutputStream zos = null;
        OutputStream out = null;

        long readyEnd = System.currentTimeMillis();
        System.out.println("准备完成，耗时：" + (readyEnd - readyStart) + " ms");
        try {
            long downStart = System.currentTimeMillis();
            System.out.println("开始下载");
            // *************  3. 根据获取到的图片的url，把图片按照想要的文件夹目录进行下载  *************
            // 根据申请人姓名创建文件夹
            File proposerFile = new File(dirOfOrder, "vo.getReal_name()");
            if (!proposerFile.exists()) {
                proposerFile.mkdirs();
            }

            // 下载申请人证明资料照片
            System.out.println("开始下载申请人证明资料照片");
            String[] prove_urls = "inmg.png,saks.png".split(",");
            // 创建证明资料照片文件夹
            File proveFile = new File(proposerFile, "zhengmingziliao");
            if (!proveFile.exists()) {
                proveFile.mkdirs();
            }
            for (int k = 0; k < prove_urls.length; k++) {
                //从文件服务器下载
//                WordExportUtil.downloadHttpUrl(DOMAIN + prove_urls[k], proveFile.toString(), File.separator + "prove" + k + ".jpg");
            }

            long downEnd = System.currentTimeMillis();
            System.out.println("下载完成，耗时：" + (downEnd - downStart) + " ms");
            long zipStart = System.currentTimeMillis();

            response.setContentType("application/x-zip-compressed");
            response.setHeader("Content-disposition", "attachment;filename=" + UUID.randomUUID() + ".zip");
            out = response.getOutputStream();
            zos = new ZipOutputStream(out);

            // *************  4. 把要压缩的文件夹路径、压缩文件输出流传入到ZipUtils.toZip方法，对文件夹进行压缩  *************
            // 对文件夹进行压缩，保留原文件夹路径
            ZipUtils.toZip(dirOfOrder, out, true);
            long zipEnd = System.currentTimeMillis();
            System.out.println("压缩完成，耗时：" + (zipEnd - zipStart) + " ms");

            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    zos.close();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // *************  5. 删除压缩前准备的中间文件  *************
        if (dirOfOrder != null) {
            try {
                FileUtils.deleteDirectory(dirOfOrder);
                System.out.println("中间文件已删除");
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("中间文件删除失败");
            }
        }
    }
}
